package com.swinboo.im.service.impl;

import com.easemob.im.server.EMService;
import com.easemob.im.server.model.EMPage;
import com.easemob.im.server.model.EMUser;
import com.huang.entity.result.HCollectionResult;
import com.huang.entity.result.HStringResult;
import com.huang.entity.result.ResponseEnum;
import com.huang.entity.result.ResultBuilder;
import com.swinboo.im.service.EMUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Swinboo
 * @date 2022-03-16 20:54
 * @description:
 */

@Service
public class EMUserServiceImpl implements EMUserService {

    @Resource
    private EMService emService;

    public HStringResult<EMUser> create(String username, String password) {
        try {
            return ResultBuilder.successHSingle(emService.user().create(username, password).block());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBuilder.failHSingle("创建用户失败");
        }


    }


    @Override
    public HStringResult<Boolean> delete(String username) {
        try {
            emService.user().delete(username).block();
            return ResultBuilder.successHSingle(Boolean.TRUE);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBuilder.failHSingle("删除失败");
        }
    }

    @Override
    public HCollectionResult<String> deleteAll() {
        try {
            return ResultBuilder.successHSCollection(emService.user().deleteAll().collectList().block());
        } catch (Exception e) {
            return ResultBuilder.failHSCollection(ResponseEnum.SERVICE_ERROR);
        }
    }

    @Override
    public HStringResult<Boolean> forceLogoutAllDevices(String username) {
        return null;
    }

    @Override
    public HStringResult<Boolean> forceLogoutOneDevice(String username, String resource) {
        return null;
    }

    @Override
    public HStringResult<EMUser> getUserInfo(String username) {
        try {
            return ResultBuilder.successHSingle(emService.user().get(username).block());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBuilder.failHSingle("获取失败");
        }
    }

    @Override
    public HStringResult<Boolean> isUserOnline(String username) {
        emService.user().isUserOnline(username).block();
        return ResultBuilder.successHSingle(Boolean.TRUE);
    }

    @Override
    public HCollectionResult<String> listAllUsers() {
        try {
            return ResultBuilder.successHSCollection(emService.user().listAllUsers().collectList().block());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBuilder.failHSCollection(ResponseEnum.SERVICE_ERROR);
        }
    }

    @Override
    public HCollectionResult<String> listUsers(int limit, String cursor) {
        try {
            return ResultBuilder.successHSCollection(emService.user().listUsers(limit, cursor).block().getValues());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBuilder.failHSCollection(ResponseEnum.SERVICE_ERROR);
        }
    }

    @Override
    public HStringResult<Boolean> updateUserPassword(String username, String password) {
        try {
            emService.user().updateUserPassword(username, password).block();
            return ResultBuilder.successHSingle(Boolean.TRUE);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBuilder.failHSingle("修改密码失败");
        }
    }
}
